﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using System.Data;
using IFRS.DataAccess.DAL;
using IFRS.Models;
using IFRS_Global;

public partial class IFRS_WIN11_4A : PageBase
{
    #region "---------- Property -----------"

    private readonly SPR_FCD dal_fcd = new SPR_FCD();
    private FCD fcd = new FCD();

    #endregion

    #region "---------- Events -----------"

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!(Page.IsPostBack || Page.IsCallback))
        {
            this.ViewMenu();
            BindControlData();
            SetErrorMessage();
            BindData();
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }

    protected void btnSAVE_NEW_Click(object sender, EventArgs e)
    {
        SaveData(true);
    }

    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData(false);
    }

    protected void btnCLEAR_Click(object sender, EventArgs e)
    {
        Response.Redirect(ResolveUrl(Request.Url.AbsoluteUri));
    }

    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN11_1S.aspx");
    }

    #endregion

    #region "---------- Methods -----------"

    private void ViewMenu()
    {
        this.MenuTitleBar = Resources.ifrsResource.Add + Resources.ifrsMenu.WIN11;
    }

    private void SetErrorMessage()
    {
        btnSAVE.ValidationGroup = "Master";
        btnSAVE_NEW.ValidationGroup = "Master";
        reqTRANSACTION_DATE.SetRequiredField(lblTRANSACTION_DATE, txtTRANSACTION_DATE, btnSAVE);
        reqACCOUNT_NO.SetRequiredField(lblACCOUNT_NO, txtACCOUNT_NO, btnSAVE);
        reqGL_ACCOUNT.SetRequiredField(lblGL_ACCOUNT, txtGL_ACCOUNT, btnSAVE);
        reqCOST_CENTER.SetRequiredField(lblCOST_CENTER, txtCOST_CENTER, btnSAVE);
        reqSTARTED_DATE.SetRequiredField(lblSTARTED_DATE, txtSTARTED_DATE, btnSAVE);
        reqEND_DATE.SetRequiredField(lblEND_DATE, txtEND_DATE, btnSAVE);
        reqCURRENCY_CODE.SetRequiredField(lblCURRENCY_CODE, txtCURRENCY_CODE, btnSAVE);
        reqUNPAIDCAPITAL.SetRequiredField(lblUNPAIDCAPITAL, txtUNPAIDCAPITAL, btnSAVE);
        reqINTEREST_RATE.SetRequiredField(lblINTEREST_RATE, txtINTEREST_RATE, btnSAVE);
        reqDAY_COUNT_METHOD.SetRequiredField(lblDAY_COUNT_METHOD, ddlDAY_COUNT_METHOD, btnSAVE);
        reqINTEREST_PAYMENT_FREQ.SetRequiredField(lblINTEREST_PAYMENT_FREQ, txtINTEREST_PAYMENT_FREQ, btnSAVE);
        reqINTEREST_PAYMENT_FREQTIMEBASE.SetRequiredField(lblINTEREST_PAYMENT_FREQTIMEBASE, ddlINTEREST_PAYMENT_FREQTIMEBASE, btnSAVE);
        reqPAYMENT_CONDITION.SetRequiredField(lblPAYMENT_CONDITION, ddlPAYMENT_CONDITION, btnSAVE);
    }

    private void BindControlData()
    {
        ddlDAY_COUNT_METHOD.SetData(DDL_TYPE.DayCountMethod, "", false, true);
        ddlINTEREST_PAYMENT_FREQTIMEBASE.SetData(DDL_TYPE.PaymentFrequencyTimeBase, "", false, true);
        ddlPAYMENT_CONDITION.SetData(DDL_TYPE.PaymentCondition, "", false, true);

        btnCOST_CENTER.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/CostCenter.aspx") + "','700','550');");
        btnGL_ACCOUNT.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/GLAccount.aspx") + "','700','550');");
        btnCURRENCY_CODE.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/Currency.aspx") + "','700','550');");
        btnCUSTOMER.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/Customer.aspx") + "','700','550');");

        txtCOST_CENTER.AddControl(hidCOST_CENTER.ClientID);
        txtCOST_CENTER.AddControl(txtCOST_CENTER.ClientID);
        txtCOST_CENTER.ControlSearch = txtCOST_CENTER.ClientID;
        txtCOST_CENTER.AddQueryString("bIsActive", "1");
        txtCOST_CENTER.SearchKey = "VWCostCenter_oCOST_CENTER_CD_oCOST_CENTER_NM";

        txtGL_ACCOUNT.AddControl(hidGL_ACCOUNT.ClientID);
        txtGL_ACCOUNT.AddControl(txtGL_ACCOUNT.ClientID);
        txtGL_ACCOUNT.ControlSearch = txtGL_ACCOUNT.ClientID;
        txtGL_ACCOUNT.AddQueryString("bIsActive", "1");
        txtGL_ACCOUNT.SearchKey = "GLAccount_oGLAccountCode_oGLAccountName";

        txtCURRENCY_CODE.AddControl(hidCURRENCY_CODE.ClientID);
        txtCURRENCY_CODE.AddControl(txtCURRENCY_CODE.ClientID);
        txtCURRENCY_CODE.ControlSearch = txtCURRENCY_CODE.ClientID;
        txtCURRENCY_CODE.AddQueryString("bIsActive", "1");
        txtCURRENCY_CODE.SearchKey = "Currency_oCurrencyCode_oCurrencyName";
        txtCURRENCY_CODE.Text = DefaultValue.CurrencyCode.THB;
        hidCURRENCY_CODE.Value = DefaultValue.CurrencyCode.THB;

        txtCUSTOMER.AddControl(hidCUSTOMER_ID.ClientID);
        txtCUSTOMER.AddControl(txtCUSTOMER.ClientID);
        txtCUSTOMER.ControlSearch = txtCUSTOMER.ClientID;
        txtCUSTOMER.AddQueryString("bIsActive", "1");
        txtCUSTOMER.SearchKey = "Customer_oCustomerID_oCustomerCode";

        SetControlReadOnly(txtIS_CONFIRMED);
    }

    private void BindData()
    {
        if (Request.QueryString["id"] == null)
            return;

        int ID = Convert.ToInt32(Request.QueryString["id"].ToString());
        DataTable dtFCD = dal_fcd.SEARCH_BYID(ID);
        List<FCD> FCDList = new List<FCD>();
        ConvertHelper.ConvertDataTableToObject(dtFCD, FCDList);
        if (dtFCD.Rows.Count > 0)
        {
            // hidSubsidyLoanID.Value = SubsidyLoanList[0].SubsidyLoanID.ToString();
            txtTRANSACTION_DATE.Text = DateTime.Now.DateToString();
            txtACCOUNT_NO.Text = FCDList[0].AccountNo;
            txtGL_ACCOUNT.Text = FCDList[0].GLAccount;
            hidGL_ACCOUNT.Value = FCDList[0].GLAccount;
            txtCOST_CENTER.Text = FCDList[0].COST_CENTER_NM;
            hidCOST_CENTER.Value = FCDList[0].CostCenter;
            txtSTARTED_DATE.Text = FCDList[0].StartedDate.DateToString();
            txtEND_DATE.Text = FCDList[0].EndDate.DateToString();
            txtCURRENCY_CODE.Text = FCDList[0].CurrencyCode;
            hidCURRENCY_CODE.Value = FCDList[0].CurrencyCode;
            txtUNPAIDCAPITAL.Text = FCDList[0].UnpaidCapital.ToString();
            txtINTEREST_RATE.Text = FCDList[0].InterestRate.ToString();
            ddlDAY_COUNT_METHOD.SelectedValue = FCDList[0].DayCountMethodID.ToString();
            txtINTEREST_PAYMENT_FREQ.Text = FCDList[0].InterestPaymentFreq.ToString();
            ddlINTEREST_PAYMENT_FREQTIMEBASE.SelectedValue = FCDList[0].InterestPaymentFreqTimeBase;
            ddlPAYMENT_CONDITION.SelectedValue = FCDList[0].PaymentConditionID.ToString();
            txtCUSTOMER.Text = FCDList[0].CustomerCode;
            hidCUSTOMER_ID.Value = FCDList[0].CustomerID.IntToString();
            txtIS_CONFIRMED.Text = (bool)FCDList[0].IsConfirmed ? Resources.ifrsField.CONFIRM : Resources.ifrsField.CREATE;
        }
    }

    private void SaveData(bool SaveNew)
    {
        try
        {
            if (txtSTARTED_DATE.Text.AsDate() > txtEND_DATE.Text.AsDate())
            {
                Utility.AlertMessage(Page, string.Format(Resources.ifrsResource.wrnCheckFromTo, lblEND_DATE.Text, lblSTARTED_DATE.Text));
                return;
            }

            fcd.TransactionDate = txtTRANSACTION_DATE.Text.StringToDateAllowNull();
            fcd.AccountNo = txtACCOUNT_NO.Text;
            fcd.GLAccount = hidGL_ACCOUNT.Value;
            fcd.CostCenter = hidCOST_CENTER.Value;
            fcd.StartedDate = txtSTARTED_DATE.Text.StringToDateAllowNull();
            fcd.EndDate = txtEND_DATE.Text.StringToDateAllowNull();
            fcd.CurrencyCode = txtCURRENCY_CODE.Text;
            fcd.UnpaidCapital = decimal.Parse(txtUNPAIDCAPITAL.Text);
            fcd.InterestRate = decimal.Parse(txtINTEREST_RATE.Text);
            fcd.DayCountMethodID = ddlDAY_COUNT_METHOD.SelectedValue.AsInt32();
            fcd.InterestPaymentFreq = decimal.Parse(txtINTEREST_PAYMENT_FREQ.Text);
            fcd.InterestPaymentFreqTimeBase = ddlINTEREST_PAYMENT_FREQTIMEBASE.SelectedValue;
            fcd.PaymentConditionID = ddlPAYMENT_CONDITION.SelectedValue.AsInt32();
            fcd.CustomerID = hidCUSTOMER_ID.Value == "" ? (int?)null : hidCUSTOMER_ID.Value.AsInt32();
            fcd.IsConfirmed = false;
            fcd.SetStandardField();

            int iID = dal_fcd.INSERT(fcd);
            if (iID > 0)
            {
                if (SaveNew)
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(Request.Url.AbsolutePath));
                else
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN11_2E.aspx?&id={0}", iID)));
            }
            else
            {
                if (iID == -1)
                    Utility.AlertMessage(Page, Resources.ifrsResource.Duplicate);
                else
                    Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    #endregion
}